<?php

namespace Drupal\jsonapi_cross_bundles;

use Drupal\jsonapi\Context\FieldResolver;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\jsonapi\IncludeResolver;
use Drupal\Core\Http\Exception\CacheableBadRequestHttpException;

class CrossIncludeResolver extends IncludeResolver {

  protected static function resolveInternalIncludePaths(ResourceType $base_resource_type, array $paths) {
    $internal_paths = array_map(function ($exploded_path) use ($base_resource_type) {
      if (empty($exploded_path)) {
        return [];
      }
      try {
        $result = FieldResolver::resolveInternalIncludePath($base_resource_type, $exploded_path);
      } catch(CacheableBadRequestHttpException $e) {
        $result = [];
      }
      return $result;
    }, $paths);
    $flattened_paths = array_reduce($internal_paths, 'array_merge', []);
    return $flattened_paths;
  }

}
